﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Tasks
{
    /// <summary>
    /// 
    /// </summary>
    public class TaskScheduler
    {
        /// <summary>
        /// 
        /// </summary>
        public TaskScheduler()
        {
            this.initialize();
        }
        /// <summary>
        /// 
        /// </summary>
        protected internal void initialize()
        {
            this.NextExecuteTime = null;
        }
        /// <summary>
        /// 
        /// </summary>
        public ScheduleType ScheduleType
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public DurationUnit DurationUnit
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public int Duration
        {
            get;
            set;
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? StartTime { get; internal set; }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? EndTime { get; internal set; }
        /// <summary>
        /// 下一次执行时间
        /// </summary>
        public DateTime? NextExecuteTime { get; protected set; }
        /// <summary>
        /// 
        /// </summary>
        public bool StopOnError { get; internal set; }
        /// <summary>
        /// 
        /// </summary>
        protected internal void SetNextExecute()
        {
            var nextTime = this.NextExecuteTime ?? DateTime.Now;
            this.SetNextExecute(nextTime);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="currentTime"></param>
        protected internal void SetNextExecute(DateTime currentTime)
        {
            if (this.ScheduleType == ScheduleType.Iteration)
            {
                switch (this.DurationUnit)
                {
                    case DurationUnit.Second:
                        this.NextExecuteTime = currentTime.AddSeconds(this.Duration);
                        break;
                    case DurationUnit.Minute:
                        this.NextExecuteTime = currentTime.AddMinutes(this.Duration);
                        break;
                    case DurationUnit.Hour:
                        this.NextExecuteTime = currentTime.AddHours(this.Duration);
                        break;
                    case DurationUnit.Day:
                        this.NextExecuteTime = currentTime.AddDays(this.Duration);
                        break;
                    case DurationUnit.Month:
                        this.NextExecuteTime = currentTime.AddMonths(this.Duration);
                        break;
                    default:
                        this.NextExecuteTime = currentTime.AddSeconds(this.Duration);
                        break;
                }
            }
        }

    }
}
